﻿#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
/// <summary>
/// 语言包逻辑代码
/// </summary>
public partial class ESF_Logic
{
    /// <summary>
    /// 获得语言包字典
    /// Key:语言包类型
    /// Value:语言包索引
    /// </summary>
    /// <param name="_defaultLang">默认语言</param>
    /// <returns>语言包字典</returns>
    public static Dictionary<enESF_AllLanguageClassify,int> GetSupportLanguages(ref enESF_AllLanguageClassify _defaultLang)
    {
        Dictionary<enESF_AllLanguageClassify, int> result = new Dictionary<enESF_AllLanguageClassify, int>();
        if (languageSetting.xml.supportLangs != null)
        {
            int enIdx = 0;
            List<ESF_Xml_LanguageSetting_Item> items = new List<ESF_Xml_LanguageSetting_Item>(languageSetting.xml.supportLangs);
            items = items.OrderByDescending((x) => { return x.isDefalut; }).ToList();            
            foreach (var key in items)
            {
                result.Add(key.classify, enIdx);
                if (key.isDefalut)
                {
                    _defaultLang = key.classify;
                }
                enIdx++;
            }
        }
        return result;
    }


    /// <summary>
    /// 获得语言包字典
    /// Key:语言包类型
    /// Value:语言包索引
    /// </summary>
    /// <param name="_defaultLang">默认语言</param>
    /// <returns>语言包字典</returns>
    public static Dictionary<enESF_AllLanguageClassify, int> GetLanguages(ref enESF_AllLanguageClassify _defaultLang)
    {
        Dictionary<enESF_AllLanguageClassify, int> result = new Dictionary<enESF_AllLanguageClassify, int>();
        if (languageSetting.xml.supportLangs != null)
        {
            int enIdx = 0;
            List<ESF_Xml_LanguageSetting_Item> items = new List<ESF_Xml_LanguageSetting_Item>(languageSetting.xml.items);
            items = items.OrderByDescending((x) => { return x.isDefalut; }).ToList();
            foreach (var key in items)
            {
                result.Add(key.classify, enIdx);
                if (key.isDefalut)
                {
                    _defaultLang = key.classify;
                }
                enIdx++;
            }
        }
        return result;
    }

    /// <summary>
    /// 生成语言包脚本
    /// </summary>
    /// <returns>true:成功,false:失败</returns>
    [ESF_OneKeyBuild(ESF_enOneKeyBuildStep.BuildLanguageHelper)]
    public static bool BuildLanguageScript()
    {
        bool result = true;
        ESF_BuildTextAsset bta = null;
        string code = string.Empty;
        #region 生成语言包脚本 XlsxDbLibraryHelper_Language

        List<string> enLangNames = new List<string>();
        List<int> enLangValues = new List<int>();
        List<string> enLangAlias = new List<string>();        
        //默认枚举名称
        string defName = string.Empty;
        if (languageSetting.xml.supportLangs != null)
        {
            foreach (var key in languageSetting.xml.supportLangs)
            {
                if (string.IsNullOrEmpty(defName) || key.isDefalut)
                {
                    defName = key.classify.ToString();
                }
            }
        }
        enESF_AllLanguageClassify defaultLan = 0;
        Dictionary<enESF_AllLanguageClassify, int> langs = GetSupportLanguages(ref defaultLan);
        foreach (var key in langs)
        {
            enLangNames.Add(key.Key.ToString());
            enLangValues.Add(key.Value);
            enLangAlias.Add(key.Key.ESF_FirstAttribute<ESF_AliasTooltipAttribute>().alias);  
        }
       
        //语言包资源[Key:枚举名称,Value:路径]       
        var abb = ESF_Logic.assetBundleBuild.xml;
        abb.Resolve();
        Dictionary<string, string> enRootFolderNames = abb.GetRootFolderEnumPathMaping();
        //Key:根目录枚举 Value:[Key:语言枚举,Value:根路径]
        Dictionary<string, Dictionary<enESF_AllLanguageClassify, string>> abRp = new Dictionary<string, Dictionary<enESF_AllLanguageClassify, string>>();
        Dictionary<enESF_AllLanguageClassify, string> tmp = null;        
        foreach (var key in abb.supportLangs)
        {
            foreach (var abf in key.assetBundleFolders)
            {
                foreach (var rp in enRootFolderNames)
                {
                    if (abf.StartsWith(rp.Value))
                    {
                        if (!abRp.TryGetValue(rp.Key, out tmp))
                        {
                            tmp = new Dictionary<enESF_AllLanguageClassify, string>();
                            abRp.Add(rp.Key, tmp);
                        }
                        tmp.Add(key.classify, abf);
                    }
                }
            }
        }
        code = File.ReadAllText(ESF_enResxTempleteFile.Compiler_Xlsx_LanguageHelper_Script_txt);
        code = code.CompilerCSharp(new
        {
            enLangNames,
            enLangValues,
            enLangAlias,
            enRootFolderNames,
            abRp,
            defaultAlternateLanguage = languageSetting.xml.defaultAlternateLanguage,
            defName,
            className= languageSetting.xml.languageHelperScriptClassName
        });
        bta = new ESF_BuildTextAsset($"{languageSetting.xml.languageHelperScriptClassName}",
            languageSetting.xml.languageHelperScriptFolder, enESF_UnityFileExt.CS, code);
        bta.CreateAsset();
        #endregion
        ESF_Utility.ExecuteMenuAssetsRefresh();
        return result;
    }
}
#endif
